package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.VehicleType;

import org.springframework.stereotype.Service;

@Service
public class VehicleTypeDAO extends BaseDAO<VehicleType, Integer>{
	private final static String sql_loadAll = "FROM VehicleType vt WHERE vt.deleted = 0 AND vt.oc = ?";
	
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM VehicleType vt WHERE 1 = 1");
		
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String oc = map.get(Parm.OC);
			String name = map.get(Parm.NAME);
			if (null != name && !"".equals(name)) {
				hql.append(" AND vt.name LIKE ?");
				list.add("%" + name + "%");
			}if(isNotNull(oc)){
				hql.append(" AND vt.oc = ?");
				list.add(Integer.parseInt(oc));
			}
		}
		
		hql.append(" ORDER BY vt.code DESC");
		
		if (findToPageBean(hql.toString(), pageBean, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		return RESULT.ERROR;
	}
	/**
	 * 修改车辆类别信息
	 */
	public RESULT update(VehicleType vehicleType) {
		if (null == vehicleType || null == vehicleType.getCode())
			return RESULT.PARAMETER_ERROR;
		
		VehicleType old = get(vehicleType.getCode());
		if (null == old)
			return RESULT.NOT_FIND;
		if(null !=(vehicleType.getName())) {
			old.setName(vehicleType.getName());
		}
		if (RESULT.SUCCESS == upd(old)) {
			return RESULT.SUCCESS;
		} else {
			return RESULT.ERROR;
		}
	}
	
	public List<VehicleType> getAll(String oc) {
		if (null == Cache.vehicleType || Cache.vehicleType.size() == 0)
			return loadAll(oc);
		return Cache.vehicleType;
	}
	
	public List<VehicleType> loadAll(String oc) {
		Cache.vehicleType = find(sql_loadAll,Integer.parseInt(oc));
		return Cache.vehicleType;
	}
	public List<VehicleType> findByOc(Integer oc) {
		List<VehicleType> list = find(sql_loadAll, oc);
		return list;
	}
	
	public List<VehicleType> findByNameOc(String name, Integer oc){
		if (name == null || oc == null) return null;
		StringBuffer hql = new StringBuffer("FROM VehicleType iat WHERE iat.name = ? AND iat.oc = ? ");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(name);
		parm.add(oc);
		
		return find(hql.toString(), parm.toArray());
	}
	public RESULT findByMapOc(Integer oc,Map<String, String> map, PageBean pb) {
		StringBuffer hql = new StringBuffer(" FROM VehicleType vt WHERE vt.oc = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(oc);
		if (map != null) {
			String name = map.get(Parm.NAME);			//车辆类型
			String cn = map.get(Parm.CN);			//车牌号
			String on = map.get(Parm.ON);		//状态
			if (null != name && !"".equals(name)) {
				hql.append(" AND vt.name LIKE ?");
				list.add("%" + name + "%");
			}if (isNotNull(cn)) {
				list.add("%" + cn + "%");
				hql.append(" AND vt.cu IN (SELECT ui.code FROM UserInfo ui WHERE ui.realName LIKE ?)");
			}if (isNotNull(on)) {
				list.add("%" + on + "%");
				hql.append(" AND vt.oc IN (SELECT o.code FROM Organ o WHERE o.name LIKE ?)");
			}
		}
		
		hql.append(" ORDER BY vt.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
}
